/**
 *
 */
package com.storm.lib.util;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;

/**
 * 类功能: 控制线程级别参数的类
 * @author johnny
 * @version 2010-11-1
 */
public class ThreadLocalUtil {
	/**
	 * 设置session参数
	 */
	private static final ThreadLocal<HashMap<String,Object>> sessionVar = new ThreadLocal<HashMap<String,Object>>();
	protected final static Logger logger = Logger.getLogger(ThreadLocalUtil.class);

	/**
	 *
	 * 方法功能:设置参数
	 * 更新时间:2010-11-1, 作者:johnny
	 * @param varName
	 * @param obj
	 */
	public static void putVar(String varName,Object obj){
		HashMap<String,Object> threadMap = getThreadMap();;
		threadMap.put(varName, obj);
	}

	/**
	 *
	 * 方法功能:得到线程映射关系
	 * 更新时间:2010-11-1, 作者:johnny
	 */
	private static HashMap<String,Object> getThreadMap() {
		HashMap<String,Object> threadMap = sessionVar.get();
		if(threadMap==null){
			threadMap = new HashMap<String, Object>();
			sessionVar.set(threadMap);
		}
		return threadMap;
	}

	/**
	 *
	 * 方法功能:获得参数
	 * 更新时间:2010-11-1, 作者:johnny
	 * @param varName
	 * @param obj
	 */
	public static Object getVar(String varName){
		HashMap<String,Object> threadMap = getThreadMap();
		return threadMap.get(varName);
	}

	/**
	 * 方法功能:清除所有信息
	 * 更新时间:2010-11-2, 作者:johnny
	 */
	public static void clearVars() {
//		logger.debug("清理了线程参数");
		sessionVar.set(null);
	}
}
